package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.InternInfo;
import com.amsp.oa.oademo.service.InternInfoService;
import com.amsp.oa.oademo.vo.InternInfoVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "新员工实习资料管理")
@RestController
@RequestMapping("/api/intern-info")
public class InternInfoController {

    @Autowired
    private InternInfoService internInfoService;

    @ApiOperation("保存或更新实习资料")
    @PostMapping("/save")
    public ResponseEntity<Map<String, Object>> saveOrUpdate(@RequestBody InternInfoVO vo) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = internInfoService.saveOrUpdateInternInfo(vo);
            result.put("code", success ? 200 : 500);
            result.put("message", success ? "保存成功" : "保存失败");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "保存失败：" + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    @ApiOperation("批量保存或更新实习资料")
    @PostMapping("/batch-save")
    public ResponseEntity<Map<String, Object>> batchSaveOrUpdate(@RequestBody List<InternInfoVO> voList) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = internInfoService.batchSaveOrUpdateInternInfo(voList);
            result.put("code", success ? 200 : 500);
            result.put("message", success ? "保存成功" : "保存失败");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "保存失败：" + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    @ApiOperation("获取实习资料详情")
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getDetail(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            InternInfoVO vo = internInfoService.getInternInfoDetail(id);
            result.put("code", 200);
            result.put("data", vo);
            result.put("message", "获取成功");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取失败：" + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    @ApiOperation("分页查询实习资料")
    @GetMapping("/page")
    public ResponseEntity<Map<String, Object>> getPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            InternInfoVO vo) {
        Map<String, Object> result = new HashMap<>();
        try {
            Page<InternInfo> page = new Page<>(current, size);
            Page<InternInfoVO> voPage = internInfoService.getInternInfoPage(page, vo);
            result.put("code", 200);
            result.put("data", voPage);
            result.put("message", "查询成功");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    @ApiOperation("删除实习资料")
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> delete(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = internInfoService.deleteInternInfo(id);
            result.put("code", success ? 200 : 500);
            result.put("message", success ? "删除成功" : "删除失败");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "删除失败：" + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    @ApiOperation("批量删除实习资料")
    @DeleteMapping("/batch")
    public ResponseEntity<Map<String, Object>> batchDelete(@RequestBody List<Long> ids) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = internInfoService.batchDeleteInternInfo(ids);
            result.put("code", success ? 200 : 500);
            result.put("message", success ? "删除成功" : "删除失败");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "删除失败：" + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }
}